<?php

/**
 * Created by PhpStorm.
 * Author: ls
 * Time: 2023/9/28 9:15
 * Desc: 通用接口
 */
class Service_Api_Common extends Contract_Api_Common
{

    public function get_code_image(): array
    {
        // 验证标识
        $uuid = Model_Api_V2_Common::create_uuid();

        // 创建验证码图片
        $pic = Model_Api_V2_Common::verify_code_image_base64();

        // 生成缓存
        Model_Api_V2_Common::cache('set', $uuid, $pic['code']);

        return array(
            'pic' => $pic['base64'],
            'key' => $uuid
        );
    }

    public function check_code_image($params): bool
    {
        /**
         * @var $captcha_key
         * @var $code
         */
        extract($params);
        if (!Model_Api_V2_Common::verify_image_code($captcha_key, $code)) {
            return false;
        } else {
            return true;
        }
    }

    public function send_msg_code($params): bool
    {
        /**
         * @var $phone
         * @var $imgCode
         * @var $captcha_key
         */
        extract($params);

        if (!Model_Api_V2_Common::verify_image_code($captcha_key, $imgCode)) {
            throw new Exception('图片码错误');
        }

        $rand       = rand(1000, 9999);
        $content    = '验证码' . $rand . '，用于游记发布，请不要把验证码泄露给其他人。';
        $result     = St_SMSService::send_msg($phone, '', $content);
        $result_arr = json_decode($result, true);
        if ($result_arr['Success']) {
            Model_Api_V2_Common::cache('set', 'uni_note_msg_code_' . $phone, $rand);
            return true;
        } else {
            throw new Exception('发送失败');
        }
    }

    public function upload_img($params): array
    {
        /**
         * @var $imgstr
         */
        extract($params);

        $path = '/uploads/uniapp/' . date('Ymd') . '/';

        // 检查uniapp文件夹是否创建
        $uniapp_file = BASEPATH . 'uploads/uniapp/';
        if (!file_exists($uniapp_file)) {
            //检查是否有该文件夹，如果没有就创建
            if (!mkdir($uniapp_file, 0755) && !is_dir($uniapp_file)) {
                throw new \RuntimeException(sprintf('Directory "%s" was not created', $uniapp_file));
            }
        }

        // 保存图片
        $result = $this->save_base64_image($imgstr, $path);

        if (!$result) {
            throw new Exception('上传失败');
        }

        return array('imgurl' => Model_Api_V2_System::uniapp_img($result));
    }

    public function upload_video($params): array
    {
        /**
         * @var $str
         */
        extract($params);

        $path = '/uploads/uniapp/' . date('Ymd') . '/';

        // 检查uniapp文件夹是否创建
        $uniapp_file = BASEPATH . 'uploads/uniapp/';
        if (!file_exists($uniapp_file)) {
            //检查是否有该文件夹，如果没有就创建
            if (!mkdir($uniapp_file, 0777) && !is_dir($uniapp_file)) {
                throw new \RuntimeException(sprintf('Directory "%s" was not created', $uniapp_file));
            }
        }

        // 保存图片
        $result = $this->save_base64_video($str, $path);

        if (!$result) {
            throw new Exception('上传失败');
        }

        return array('url' => $result);
    }
}